import store from '@/store';
import base from "@/config/baseUrl";
import Vue from 'vue'
// 获取链接参数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}
// h5登录
export const h5Login = (callback) => {

  
			if (base.publicAppId==''){
				uni.showModal({
					title:Vue.prototype.$i18n.t('tips'),
					content:Vue.prototype.$i18n.t('nologinjumpurl'),
					confirmText: Vue.prototype.$i18n.t('nowlogin'),
					cancelText: Vue.prototype.$i18n.t('waitminute'),
					success: (res) => {
						if(res.confirm){
							uni.navigateTo({
								url: "/pages/user/account/login/login"
							});
						}
					}
				});
				
				return
			}
    if (isWechat()) {
        // 公众号登录
        var local = encodeURIComponent(window.location.href);
        var url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + base.publicAppId + "&redirect_uri=" + local + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"
        var code = getUrlParam("code");
        if (!code) {
            window.location = url;
        } else {
            var httpData = {
                code: code
            }
            uni.$u.http.post('/user/login', httpData).then((res) => {
                let userInfo = {
                    ...res,
                    token:true,//token用于判断是否登录
                }
                store.commit('setUserInfo',userInfo)
                callback && callback(res)
            })
        }
    } else {
        // 通用登录页--验证码登录
        uni.showModal({
        	title:"TIPS",
        	content:"You have not logged in yet, please log in first~",
        	confirmText: "Sign in",
        	cancelText: "Wait a minute",
        	success: (res) => {
        		if(res.confirm){
        			uni.navigateTo({
        				url: "/pages/user/account/login/login"
        			});
        		}
        	}
        });
    }
}
// 浏览器判断是否微信
export const isWechat = () => {
//#ifdef H5
	let ua = window.navigator.userAgent.toLowerCase();
	if (ua.match(/MicroMessenger/i) == "micromessenger") {
		//console.log('是微信客户端')  
		return true;  
	}else{
        //console.log('不是微信客户端')  
        return false;  
    }
//#endif
// #ifdef MP
   return true;  
//#endif
};

//微信浏览器中打开H5隐藏顶部导航方法
export const hidePageNavInWechatBrowser = () => {
  //#ifdef H5
  if (isWechat()) {
    let pageNav = document.getElementsByTagName("uni-page-head");
    if (pageNav && pageNav[0]) {
      pageNav[0].style.display = "none";
    }
  }
  //#endif

	//微信浏览器环境下隐藏导航方法
	//onReady() {
		//hidePageNavInWechatBrowser();
	//}
};